package com.clhbolg.match;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.clhbolg.domain.Resource;
import com.clhbolg.service.IResourceService;
import com.ruoyi.common.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author cailinhu
 * @Description TODO
 * @Date 2023/2/17 10:43
 * @Version 1.0
 */
@Component
//@Order(2)
public class ExcludesInterceptor {

    @Autowired
    SecurityProperties securityProperties;

    @Autowired
    IResourceService resourceService;

    @PostConstruct
    public void loadResourceRoleList(){
        updateSoToken();
    }
    /**
     * 调用频率：
     *      1、启动项目初始化的时候
     *      2、修改接口信息的时候
     */
    public void updateSoToken(){
        List<Resource>  resources=resourceService.getResourceByIsAnonymous(1);
        Set<String> urls = resources.stream().map(Resource::getUrl).collect(Collectors.toSet());
        ArrayList<String> excludes = CollUtil.toList(securityProperties.getExcludes());
        urls.addAll(excludes);
        securityProperties.setExcludes(ArrayUtil.toArray(urls,String.class));
    }



    public void removeSecurityItem(Resource resource){
        String[] excludes = securityProperties.getExcludes();
        ArrayList<String> strings = CollUtil.toList(excludes);
        strings.remove(resource.getUrl());
        securityProperties.setExcludes(ArrayUtil.toArray(strings,String.class));
    }





}
